package org.example.shiro.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.shiro.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author sky
 * @date 2025年03月25日 16:51
 * @description
 */
@Repository
public interface UserMapper extends BaseMapper<User>{
    /**
     * 根据用户查询对应角色信息
     * @param principal
     * @return
     */
    @Select("select name from role where id in(select rid from role_user where uid=(select id from user where name = #{principal}))")
    List<String> getUserRoleInfoMapper(String principal);

    /**
     * 根据角色名查询对应权限信息
     * @param roles
     * @return
     */
    @Select({
            "<script>",
            "select info from permissions where id in",
            "(select pid from role_ps where rid in (",
            "select id from role where name in",
            "<foreach collection='roles' item='name' open='(' separator=',' close=')'>",
            "#{name}",
            "</foreach>",
            "))",
            "</script>"
    })
    List<String> getUserPermissionInfoMapper(@Param("roles")List<String> roles);
}
